<template>
  <view class="min-h-screen w-[750rpx] bg-gray-50 mx-auto">
    <backView />

    <!-- 页面标题 -->
    <view class="bg-[#27c530] text-white p-[30rpx] rounded-b-[20rpx]">
      <text class="text-[32rpx] font-bold text-center block">全面评估</text>
    </view>

    <view class="p-[24rpx] pb-[120rpx]">
      <!-- 评估入口 -->
      <view class="bg-white rounded-[20rpx] shadow-md mb-[24rpx] overflow-hidden">
        <view class="bg-[#f8f9fa] p-[24rpx] border-b border-gray-100">
          <text class="text-[28rpx] font-bold text-gray-800">健康评估</text>
        </view>
        <view class="p-[24rpx]">
          <nut-button type="success" size="large" block @click="startAssessment" class="mb-[16rpx]"> 开始全面健康评估 </nut-button>
          <text class="text-[24rpx] text-gray-600 text-center block"> 包含诊断信息、全科健康评估、专科评估、治疗计划等完整评估内容 </text>
          
          <!-- 调试信息 -->
          <view class="mt-[16rpx] p-[12rpx] bg-gray-100 rounded-[8rpx]">
            <text class="text-[20rpx] text-gray-600">调试信息:</text>
            <text class="text-[18rpx] text-gray-500 block">记录ID: {{ recordId || '无' }}</text>
            <text class="text-[18rpx] text-gray-500 block">身份证号: {{ idNumber || '无' }}</text>
            <text class="text-[18rpx] text-gray-500 block">数据状态: {{ medicalRecordStore.getData ? '已加载' : '未加载' }}</text>
            
            <!-- 测试按钮 -->
            <view class="mt-[12rpx]">
              <nut-button type="warning" size="small" @click="testLoadData" class="w-full" v-if="recordId">
                重新加载数据
              </nut-button>
            </view>
          </view>
        </view>
      </view>

      <!-- 功能说明 -->
      <view class="bg-white rounded-[20rpx] shadow-md overflow-hidden">
        <view class="bg-[#f8f9fa] p-[24rpx] border-b border-gray-100">
          <text class="text-[28rpx] font-bold text-gray-800">评估内容</text>
        </view>
        <view class="p-[24rpx] space-y-[16rpx]">
          <view class="flex items-start">
            <text class="text-[24rpx] text-green-600 mr-[12rpx]">•</text>
            <text class="text-[24rpx] text-gray-700 flex-1">基本信息：就诊时间、主诉症状等</text>
          </view>
          <view class="flex items-start">
            <text class="text-[24rpx] text-green-600 mr-[12rpx]">•</text>
            <text class="text-[24rpx] text-gray-700 flex-1">诊断信息：诊断结果、诊断依据</text>
          </view>
          <view class="flex items-start">
            <text class="text-[24rpx] text-green-600 mr-[12rpx]">•</text>
            <text class="text-[24rpx] text-gray-700 flex-1">全科健康评估：基础指标、疾病病症、生理功能等</text>
          </view>
          <view class="flex items-start">
            <text class="text-[24rpx] text-green-600 mr-[12rpx]">•</text>
            <text class="text-[24rpx] text-gray-700 flex-1">专科评估：专业医疗评估</text>
          </view>
          <view class="flex items-start">
            <text class="text-[24rpx] text-green-600 mr-[12rpx]">•</text>
            <text class="text-[24rpx] text-gray-700 flex-1">治疗计划：处方、处置、健康教育</text>
          </view>
          <view class="flex items-start">
            <text class="text-[24rpx] text-green-600 mr-[12rpx]">•</text>
            <text class="text-[24rpx] text-gray-700 flex-1">病史记录：手术史、过敏史、家族病史</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import Taro from "@tarojs/taro";
import backView from "@/components/backView.vue";
import { localStg } from "@/utils";
import { useMedicalRecordStore } from '@/store/modules/medicalRecord'

const medicalRecordStore = useMedicalRecordStore()
const recordId = ref('')
const idNumber = ref('')

onMounted(async () => {
  console.log("onMounted");
  
  // 获取路由参数
  const params = Taro.getCurrentInstance().router?.params
  console.log('路由参数:', params)
  
  if (params?.id) {
    recordId.value = params.id
    console.log('获取到记录ID:', recordId.value)
    
    // 根据ID请求详情并保存到store
    await medicalRecordStore.loadFromPastMedicalInfo(params.id)
  } else {
    console.log('没有记录ID参数')
  }
});


function startAssessment() {
  // 优先使用从记录中获取的身份证号
  let targetIdNumber = idNumber.value
  
  // 如果没有从记录中获取到身份证号，则使用本地存储的
  if (!targetIdNumber) {
    const demographicInfo = localStg.get("demographicInfo");
    targetIdNumber = demographicInfo?.idNumber || "110101199001011234";
  }
  
  try {
    console.log("开始评估，身份证号:", targetIdNumber);
    console.log("记录ID:", recordId.value);

    // 如果有记录ID，跳转到sub-form页面；否则跳转到edit页面
    if (recordId.value) {
      Taro.navigateTo({
        url: `/pages/medical-record/sub-form?type=allCareAssessment&id=${recordId.value}&idNumber=${targetIdNumber}`,
      });
    } else {
      Taro.navigateTo({
        url: `/pages/medical-record/edit?idNumber=${targetIdNumber}`,
      });
    }
  } catch (error) {
    console.error("跳转失败:", error);
    // 使用默认值
    Taro.navigateTo({
      url: `/pages/medical-record/edit?idNumber=${targetIdNumber}`,
    });
  }
}

/**
 * 测试重新加载数据
 */
async function testLoadData() {
  if (recordId.value) {
    console.log('测试重新加载数据，ID:', recordId.value)
    await loadRecordData(recordId.value)
  }
}
</script>
